<template>
  <view class="container">
    <view class="form-item">
      <text>收货人：</text>
      <input v-model="address.addressHost" placeholder="请输入收货人姓名"></input>
    </view>
    <view class="form-item">
      <text>收货电话：</text>
      <input v-model="address.addressHostTel" placeholder="请输入收货人电话"></input>
    </view>
    <view class="form-item">
      <text>收货地址：</text>
      <input v-model="address.addressCity" placeholder="请输入收货地址"></input>
    </view>
    <view class="form-item">
		<text>标签选择：</text>
		<u-radio-group v-model="typeValue" @change="typeGroupChange">
			<u-radio 
				@change="typeChange" 
				v-for="(item, index) in list" :key="index" 
				:name="item.type"
				:disabled="item.disabled"
				active-color="#dea01f"
			>
				{{item.type}}
			</u-radio>
		</u-radio-group>
    </view>
	<view class="form-item">
		<text>默认地址：</text>
		<u-radio-group v-model="defaultAddress" @change="defaultAddressGroupChange">
			<u-radio 
				@change="defaultAddressChange" 
				v-for="(item, index) in listt" :key="index" 
				:name="item.type"
				:disabled="item.disabled"
				active-color="#dea01f"
			>
				{{item.type}}
			</u-radio>
		</u-radio-group>
	</view>
    <view class="form-item">
      <text>详细地址：</text>
      <textarea v-model="address.addressArea" placeholder="请输入详细地址"></textarea>
    </view>
    <button @click="submitForm(address.addressId)" style="background-color: #dea01f;">保存</button>
  </view>
</template>

<script>
	import { store } from '@/uni_modules/uni-id-pages/common/store.js'
		import { get, post, removeToken } from '@/utils/api.js';
export default {
  data() {
    return {
	  typeValue: '',
	  defaultAddress:'',
	  isAdd:true, //判断是编辑还是新增
	  address:{},
			list: [
				{
					type: '家',
					disabled: false
				},
				{
					type: '学校',
					disabled: false
				},
				{
					type: '公司',
					disabled: false
				},
				{
					type:'其他',
					disabled:false
				}
			],
			listt: [
				{
					type: '是',
					disabled: false
				},
				{
					type: '否',
					disabled: false
				}
			],
    };
  },
  created() {
	  if(uni.getStorageSync('addressId'))
	  {
		  //编辑收货地址
		  this.isAdd = true
		this.getAddress(uni.getStorageSync('addressId'))
	  }
	  else{
		  this.isAdd = false
	  }
        uni.removeStorageSync('addressId') 
    
  },
  methods: {
	      validateForm() {
	        const { addressHost, addressHostTel, addressCity, addressArea, typeValue, defaultAddress } = this.address;
	        const telRegex = /^1[3-9]\d{9}$/; // 中国大陆手机号码正则表达式
	        
	        if (!addressHost) {
	          uni.showToast({
	            title: '请填写收货人姓名',
	            icon: 'none'
	          });
	          return false;
	        }
	        
	        if (!addressHostTel || !telRegex.test(addressHostTel)) {
	          uni.showToast({
	            title: '请填写正确的11位手机号码',
	            icon: 'none'
	          });
	          return false;
	        }
	        
	        if (!addressCity) {
	          uni.showToast({
	            title: '请填写收货地址',
	            icon: 'none'
	          });
	          return false;
	        }
	        
	        if (!addressArea) {
	          uni.showToast({
	            title: '请填写详细地址',
	            icon: 'none'
	          });
	          return false;
	        }
	        
	        // if (!typeValue) {
	        //   uni.showToast({
	        //     title: '请选择标签',
	        //     icon: 'none'
	        //   });
	        //   return false;
	        // }
	        
	        if (defaultAddress === '') {
	          uni.showToast({
	            title: '请选择是否为默认地址',
	            icon: 'none'
	          });
	          return false;
	        }
	        
	        return true;
	      },
	  getAddress(addressId) {
	  	post('/address/getAddressInfo', {
	  	  addressId: addressId,
	  	}, {
	  	  needToken: true, 
	  	  success: (res) => {
	  		if (res.statusCode === 200) {
	            this.address = res.data.data
				this.typeValue = res.data.data.addressType
				if(res.data.data.addressStatus === true)
				{
					this.defaultAddress = '是'
				}
				else
				{
					this.defaultAddress = '否'
				}
	  		  console.log('获取收货地址成功');
	  		}
	  	  }
	  	});
	  },
		undateAddress(addressId) {
			let formData = {
			  addressHost: this.address.addressHost,
			  addressHostTel: this.address.addressHostTel,
			  addressCity: this.address.addressCity,
			  addressType: this.typeValue,
			  addressStatus: this.defaultAddress === '是' ? 1 : 0,
			  addressArea: this.address.addressArea,
			  addressId: addressId,
			  addressUser:store.userInfo.username
			};

			// 发送请求时，确保 Content-Type 设置为 application/json
			post('/address/updateAddressInfo', formData, {
			  needToken: true,
			  headers: { // 明确设置 Content-Type
				'Content-Type': 'application/json'
			  },
			  success: (res) => {
				if (res.statusCode === 200) {
				  console.log('更新收货地址成功');
				}
			  }
			});
		},
		insertAddress() {
			let formData = {
			  addressHost: this.address.addressHost,
			  addressHostTel: this.address.addressHostTel,
			  addressCity: this.address.addressCity,
			  addressType: this.typeValue,
			  addressStatus: this.defaultAddress === '是' ? 1 : 0,
			  addressArea: this.address.addressArea,
			  addressUser:store.userInfo.username
			};
		
			// 发送请求时，确保 Content-Type 设置为 application/json
			post('/address/addAddress', formData, {
			  needToken: true,
			  headers: { // 明确设置 Content-Type
				'Content-Type': 'application/json'
			  },
			  success: (res) => {
				if (res.statusCode === 200) {
				  console.log('添加收货地址成功');
				}
			  }
			});
		},
		
	  		// 选中某个单选框时，由radio时触发
	  		typeChange(e) {
	  			// console.log(e);
	  		},
	  		// 选中任一radio时，由radio-group触发
	  		typeGroupChange(e) {
	  			// console.log(e);
	  		},
			// 选中某个单选框时，由radio时触发
			defaultAddressChange(e) {
				// console.log(e);
			},
			// 选中任一radio时，由radio-group触发
			defaultAddressGroupChange(e) {
				// console.log(e);
			},
    submitForm(addressId) {
		if(this.validateForm() && this.isAdd)
		{
		this.undateAddress(addressId)
      // 在这里可以将表单数据提交到后端或进行其他操作
       uni.redirectTo({
       url:'/pages/DelAddress/DelAddress'
       })
		}
		if(this.validateForm() && !this.isAdd){
			this.insertAddress()
			uni.redirectTo({
			url:'/pages/DelAddress/DelAddress'
			})
		}

    }
  }
};
</script>

<style  lang="scss">
.container {
  padding: 20px;
}

.form-item {
  margin-bottom: 10px;
}

input,
textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
}
  .text {
    font-size: 12px;
    color: #666;
    margin-top: 5px;
  }

  .uni-px-5 {
    padding-left: 10px;
    padding-right: 10px;
  }

  .uni-pb-5 {
    padding-bottom: 10px;
  }
</style>